package cn.life.common.controller;

import cn.jiguang.common.resp.BaseResult;
import cn.life.common.utils.FileUploadUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description:
 *
 * @Author 李佳明
 * @Date 2018-08-17 14:40
 */
@RestController
@RequestMapping("image")
public class FileController {

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");

        String fileName = new Date().getTime() + "-" + file.getOriginalFilename();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String fileThumbnailName =  "thumbnail-" + new Date().getTime() + "-" + file.getOriginalFilename();
        String dateStr =  dateFormat.format(new Date());
        String imageUrl = request.getServletContext().getRealPath("/") + "images/" + dateStr;

        //判断文件夹是否存在，不存在则直接创建
        File targetFile=new File(imageUrl,fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        try{
            file.transferTo(targetFile);
        }catch(Exception e){
            e.printStackTrace();
        }
        /**
         * 缩略图begin
         */

        //拼接后台文件名称
        String thumbnailPathName = fileThumbnailName;
        //added by yangkang 2016-3-30 去掉后缀中包含的.png字符串
        if(thumbnailPathName.contains(".png")){
            thumbnailPathName = thumbnailPathName.replace(".png", ".jpg");
        }
        long size = file.getSize();
        double scale = 1.0d ;
        if(size >= 200*1024){
            if(size > 0){
                scale = (200*1024f) / size  ;
            }
        }


        //拼接文件路劲
        String thumbnailFilePathName = imageUrl + "/" + thumbnailPathName;
        File thumbnailFile = new File(thumbnailFilePathName);
        try {
            //added by chenshun 2016-3-22 注释掉之前长宽的方式，改用大小
            //Thumbnails.of(filePathName).size(width, height).toFile(thumbnailFilePathName);
            if(size < 200*1024){
                Thumbnails.of(targetFile).scale(1f).outputFormat("jpg").toFile(thumbnailFile);
            }else{
                Thumbnails.of(targetFile).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(thumbnailFile);
            }
            return dateStr + "/" + thumbnailPathName;
        } catch (Exception e1) {
            e1.printStackTrace();
            return null;
        }
        /**
         * 缩略图end
         */

    }
}
